import logging
from datetime import *

from tornado.web import RequestHandler
from tornado.gen import coroutine
from tornado.web import authenticated
from handlers import BaseHandler, UserBaseHandler

from util.function import not_need_login


class IndexHandler(BaseHandler):

    def get(self):
        self.render('site/index.html')

class GaoFenHandler(BaseHandler):

    def get(self):
        self.render('site/gf.html')


class HomeHandler(UserBaseHandler):

    @coroutine
    def get(self):
        username = self.current_user.get('username')

        dataset_cur = self.db.dataset.find({"status": 20})
        datasets = yield dataset_cur.to_list(5)

        cur_apps_my = self.db.apps.find(
            {"reseach.initiator": username, "status": 0})
        apps_my = yield cur_apps_my.to_list(5)

        records_cur = self.db.records.find()
        records = yield records_cur.to_list(5)

        apps_other_cur = self.db.apps.find(
            {"reseach.initiator": {"$ne": username}})
        apps_other = yield apps_other_cur.to_list(5)

        self.render('site/home.html', datasets=datasets,
                    records=records, apps_my=apps_my, apps_other=apps_other)
